﻿#ifndef MAINWINDOW_H
#define MAINWINDOW_H

#include <QCoreApplication>
#include <QMainWindow>
#include <QTimer>
#include <QMessageBox>
#include <QEvent>
#include <QMouseEvent>
#include <QKeyEvent>
#include <QCloseEvent>
#include <Windows.h>
#include <QtTest/QTest>

#include <opencv2/opencv.hpp>

#include "tool.h"
#include "mythreadone.h"
#include "uim.h"
#include "Config.h"
#include "LoadSimCanFunc.h"
#include "setup.h"

#pragma execution_character_set("utf-8")

QT_BEGIN_NAMESPACE
namespace Ui {
class MainWindow;
}
QT_END_NAMESPACE

using namespace cv;
using namespace std;

class MainWindow : public QMainWindow
{
    Q_OBJECT

public:
    MainWindow(QWidget *parent = nullptr);
    ~MainWindow();

protected:
    void keyPressEvent(QKeyEvent *event) override;
    void keyReleaseEvent(QKeyEvent *event) override;
    void closeEvent(QCloseEvent *event) override;

private slots:
    void recvSetupSave();
    //设置
    void on_pushButtonSetup_clicked();
    //退出
    void on_pushButtonExit_clicked();
    //关机
    void on_pushButtonClose_clicked();
    //刷新IN
    void on_pushButtonForBackSetIn_clicked();
    //刷新OUT
    void on_pushButtonForBackSetOut_clicked();
    //移动复位
    void on_pushButtonForBackReset_clicked();
    //移动IN
    void on_pushButtonForBackMoveIn_clicked();
    //移动OUT
    void on_pushButtonForBackMoveOut_clicked();
    //上-按住
    void on_pushButtonForBackUp_pressed();
    //上-释放
    void on_pushButtonForBackUp_released();
    //下-按住
    void on_pushButtonForBackDown_pressed();
    //下-释放
    void on_pushButtonForBackDown_released();
    //压片-下压
    void on_pushButtonPushDown_clicked();
    //压片-上抬
    void on_pushButtonPushUp_clicked();
    //压片-复位
    void on_pushButtonPushReset_clicked();
    //快左
    void on_pushButtonLeftFast_clicked();
    //快右
    void on_pushButtonRightFast_clicked();
    //向左
    void on_pushButtonLeftMove_clicked();
    //向右
    void on_pushButtonRightMove_clicked();
    //左-按住
    void on_pushButtonLeftMove_pressed();
    //左-释放
    void on_pushButtonLeftMove_released();
    //右-按住
    void on_pushButtonRightMove_pressed();
    //右-释放
    void on_pushButtonRightMove_released();

    void on_spinBoxUimLeftStep_valueChanged(int arg1);

    void on_spinBoxUimRightStep_valueChanged(int arg1);

    // void on_spinBoxUimForBackSetIn_valueChanged(int arg1);

    // void on_spinBoxUimForBackSetOut_valueChanged(int arg1);

    // void on_spinBoxUimPushUp_valueChanged(int arg1);

    // void on_spinBoxUimPushDown_valueChanged(int arg1);

    void on_spinBoxUimRate_valueChanged(int arg1);

    void on_spinBoxUimFastRate_valueChanged(int arg1);

    // void on_pushButtonLeftAdd_clicked();

    // void on_pushButtonLeftDec_clicked();

    // void on_pushButtonRightAdd_clicked();

    // void on_pushButtonRightDec_clicked();

    void on_pushButtonLeftRev_clicked();

    void on_pushButtonRightRev_clicked();

private slots:
    void updatePressTimer(void);
    void showBottomTimer(void);
    void procUimOver(bool ok);
    void retCanMsg(QString,int,int);
    void updateCanMoveTimer(void);

private:
    Ui::MainWindow *ui;

    void init(void);
    void exit(void);

    void initConfVar(void);
    void initUimVar(void);
    void initTime(void);
    void initScreen(void);

    void initUimScreen(void);
    void initUimDriven(bool open);

    void initCanMoveTimer(void);
    void canMoveTimerStart(bool en=false);

    void showBottomRateInfo(void);
    void showBottomLeftInfo(void);
    void showBottomRightInfo(void);
    // void showBottomForBackInfo(void);
    // void showBottomPushInfo(void);

    // void showBottomUimLeftPos(void);
    // void showBottomUimRightPos(void);
    // void showBottomUimForBackPos(void);
    // void showBottomUimPushPos(void);
    void showBottomUimState(const QString msg);

    bool isPush(void);

    Config *conf;
    UIM *uim;
    MyThreadOne *myThread;

    QTimer *bottomTimer;
    bool isFirstMaxWindow;
    bool isShutdown;
    bool isCloseState;
    int uimStart;
    int photoMove;      //0-无，1-快左，2-向左一步，3-向左连续，4-向右连续，5-向右一步，6-快右
    int curMove;        //0-无,1-forback,2-push
    int curForbackReset;
    int curPushReset;

    QTimer  *canMoveTimer;
    qint64  curCanMoveTimer;
    int curCanMoveQec;
    bool isKeyCanMove;

    QTimer  *pressTimer;
    bool isPress;
    int curPressMode;

signals:
    void SendRTCNMsg(QString,int,int);

private:
    void photoLeftMoveOne(bool slow=false);
    void photoRightMoveOne(bool slow=false);
    void photoLeftMove(void);
    void photoRightMove(void);

    void canMovePos(const int id,const QString msg,const int locate,int rate,const int direction);
    void canMove(const QString msg,const bool mode,const int direction);
    void canStopMove(void);

    void photoStopMove(void);
    void photoScreenMove(void);

    // bool getCanForBackLimitPos(const int direction=0);
    bool setCanFar(int node,int rate,int step);
    //回调函数
    void canCallback(void);
    void RcvRtcnNotify(unsigned int dwDevIndex, P_CAN_MSG_OBJ pRtcnMsgObj, unsigned int dwMsgLen);
    static void StaticRcvRtcnNotify(unsigned int dwDevIndex, P_CAN_MSG_OBJ pRtcnMsgObj, unsigned int dwMsgLen);

    void procCanForBackSense(int code, QString ts = QString("forback"));
    void procCanPushSense(int code, QString ts = QString("push"));

    bool isStepMax(int code);
    bool isStepOrg(int code);
    bool isStepMove(int code);
    bool isSenseS1(int code);
    bool isSenseS2(int code);
    bool isSenseS3(int code);
    int getSense2(int code,int s1,int s2);
    // int getSense3(int code,int s1,int s2,int s3);
    int getRevStep(int curStep,int step);

};
#endif // MAINWINDOW_H
